package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.thirdparty.WxNotifyMsgSetInfo;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.IArticleService;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.UserGroupGzhService;
import com.tulele.vtool.model.UserGroupOpenId;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SysConstant;
import com.tulele.vtool.util.ToolUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;

//11:20 文章推送
@Component
@Slf4j
public class NextArticlePushAsyncTask {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private IUserService userService;

    @Autowired
    private UserGroupGzhService userGroupGzhService;

    //20200401 hyl 改成同步发 因为多线程 数据库顶不住
    //20200512 调整参数，多线程跑
    @Async("taskExecutor")
    public void execute(String redisKey, UserInfo userInfo, String profile) throws Exception {


        WxPushUserInfo wxPushUserInfo = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {

            WxNotifyMsgSetInfo wxNotifyMsgSetInfo = userService.getUserWxMsgAcceptSwitchInfo(wxPushUserInfo.getUserId());

            int fifthTimeArticleAcceptStatus = Optional.ofNullable(wxNotifyMsgSetInfo.getFifthTimeArticleAcceptStatus()).orElse(1);

            if (fifthTimeArticleAcceptStatus == 1) {

                //String kefuMsg = articleService.generateUserPushArticleInfoV3(wxPushUserInfo.getUserId(), 5);
                String kefuMsg = articleService.generateUserPushArticleInfoNext(wxPushUserInfo.getUserId(), profile);
                if (!kefuMsg.equals("")) {
                    if (userInfo.getPhoneModel().equals(SysConstant.IOS)) {
                        kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                    }
                    WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                    try {
                        WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                        if (weixinService == null) {
                            weixinService = objectFactory.getObject();
                            weixinService.reSetConfigStorage(profile);
                            WeixinServiceMap.putWeixinService(profile, weixinService);
                        }
                        weixinService.getKefuService().sendKefuMessage(message);
                    } catch (WxErrorException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Async("taskExecutor")
    public void executeVideo(String redisKey, UserInfo userInfo, String profile) throws Exception {

        WxPushUserInfo wxPushUserInfo = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {

            WxNotifyMsgSetInfo wxNotifyMsgSetInfo = userService.getUserWxMsgAcceptSwitchInfo(wxPushUserInfo.getUserId());

            int fifthTimeArticleAcceptStatus = Optional.ofNullable(wxNotifyMsgSetInfo.getFifthTimeArticleAcceptStatus()).orElse(1);

            if (fifthTimeArticleAcceptStatus == 1) {
                String kefuMsg = articleService.generateUserPushVideoNext(wxPushUserInfo.getUserId(), profile);
                if (!kefuMsg.equals("")) {
                    if (userInfo.getPhoneModel().equals(SysConstant.IOS)) {
                        kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                    }
                    WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                    try {
                        WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                        if (weixinService == null) {
                            weixinService = objectFactory.getObject();
                            weixinService.reSetConfigStorage(profile);
                            WeixinServiceMap.putWeixinService(profile, weixinService);
                        }
                        weixinService.getKefuService().sendKefuMessage(message);
                    } catch (WxErrorException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Async("taskExecutor")
    public void execute_10sec(UserInfo userInfo, String profile) throws Exception {

        WxPushUserInfo wxPushUserInfo = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {

            String kefuMsg = articleService.generateUserPushArticleInfo_10sec(wxPushUserInfo.getUserId(), profile);
            if (!kefuMsg.equals("")) {
                if (userInfo.getPhoneModel().equals(SysConstant.IOS)) {
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                try {
                    WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                    if (weixinService == null) {
                        weixinService = objectFactory.getObject();
                        weixinService.reSetConfigStorage(profile);
                        WeixinServiceMap.putWeixinService(profile, weixinService);
                    }
                    weixinService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
